<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('修改')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="600px" top="10vh" @close='close'>
    <div class="dialogContentBoxMaxHeight80">
      <el-form ref="form" :model="record" label-width="130px" :rules="rules" label-position="left" style="margin:0 20px;">
        <el-form-item :label="$t('管理员名称')" prop="name">
          {{record.name}}
        </el-form-item>
        <el-form-item :label="$t('密码')" prop="password">
          <el-input v-emoji v-model="record.password" style="width:300px;" size='small' @focus="handleFocus" @blur="handleBlur" class="inputPsd"></el-input>
          <div class="adminConfigPassword">{{$t('密码至少8位，至多24位，至少使用数字、小写字母、大写字母、特殊符号中的三种，并且密码字符中不应包含管理员名称！')}}</div>
        </el-form-item>
        <el-form-item :label="$t('确认密码')" prop="password_agree">
          <el-input v-emoji v-model="record.password_agree" style="width:300px;" size='small' @focus="handleFocus" @blur="handleBlur" class="inputPsd"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit">{{$t('确 定')}}</el-button>
      <el-button @click="close">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { editPassword } from '@/api/system/adminConfig/administrators'
import Middle from '@/utils/middle.js'
export default {
  name: 'AdminConfigDialog',
  data () {
    var validatePassword = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入密码')))
      } else {
        callback()
      }
    }
    var validatePasswordAgree = (rule, value, callback) => {
      if (this.record.password !== this.record.password_agree) {
        callback(new Error(this.$t('密码两次输入的不一致,请仔细确认!')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        name: '',
        password: '',
        password_agree: ''
      },
      recordCopy: {
        name: '',
        password: '',
        password_agree: ''
      },
      rules: {
        password: [{ required: false, validator: validatePassword, trigger: 'blur' }],
        password_agree: [{ required: false, validator: validatePasswordAgree, trigger: 'blur' }]
      }
    }
  },
  created () {
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.record.name = data
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // this.record.port = Number(this.record.port)
          editPassword({ name: this.record.name, password: this.record.password }).then(res => {
            if (res.status === 0) {
              Middle.$emit('logout')
              this.$message({
                message: this.$t('修改成功'),
                type: 'success'
              })
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.$parent.adminConfigDialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    handleFocus () {
      this.$refs.password = 'password'
    },
    handleBlur () {
      this.$refs.password = 'text'
    }
  }
}
</script>
<style lang="scss" scoped>
.adminConfigPassword {
  font-size: 12px;
  line-height: 20px;
  color: #999;
}
.inputPsd {
  -webkit-text-security: disc;
}
</style>
